
const { controller_save,
    controller_find, controller_delete,
    controller_get } = require("../../../controller/custManager/vipLevelConfig/vipLevel.CONTROLLER");
const { serverError } = require("../../../utils");
const { emptyCheck } = require("../../../utils/checkEmpty");

/**
 * @description 会员制度 路由
 * @author 韩
 */
const router = require("koa-router")()
router.prefix("/custManager/vipLevel");

/**@description 添加或编辑 */
router.post("/create", async (ctx, next) => {
    try {
        const {
            id = undefined,
            levelName = undefined,
            levelCode = undefined,
            levelIcon = undefined,
            vipDiscount = undefined,
            vipDiscountType = undefined,
            vipDiscountValueType = undefined,
            vipDiscountValue2 = undefined,
            vipDiscountValue2Type = undefined,
            vipDiscountValue3 = undefined,
            vipDiscountValue3Type = undefined,
            vipDiscountValue4 = undefined,
            upgradeCondition = undefined,
            validityPeriod = undefined,
            isDefault = undefined,
            freeShipping = undefined,
            pointMultiplier = undefined,
            description = undefined,
        } = ctx.request.body
        const check = emptyCheck({})
        if (check.code !== 0) {
            ctx.body = check
        } else {
            const res = await controller_save({
                id,
                levelName,
                levelCode,
                levelIcon,
                vipDiscount,
                vipDiscountType,
                vipDiscountValueType,
                vipDiscountValue2,
                vipDiscountValue2Type,
                vipDiscountValue3,
                vipDiscountValue3Type,
                vipDiscountValue4,
                upgradeCondition,
                validityPeriod,
                isDefault,
                freeShipping,
                pointMultiplier,
                description,
            })
            ctx.body = res
        }
    } catch (error) {
        console.log(error)
        ctx.body = serverError
    }
})




/**@description 根据id获取详情 */
router.get("/get", async (ctx, next) => {
    try {
        const id = ctx.query.id || undefined;
        const res = await controller_get({ id });
        ctx.body = res;
    } catch (error) {
        ctx.body = serverError
    }
})

/**@description 根据id删除 */
router.delete("/delete", async (ctx, next) => {
    try {
        const id = ctx.query.id || undefined;
        const res = await controller_delete({ ids: [id], id });
        ctx.body = res;
    } catch (error) {
        ctx.body = serverError
    }
})


// 查询列表
router.post("/list", async (ctx, next) => {
    try {
        const {
            current,
            pageSize,
            levelName
        } = ctx.request.body;
        const res = await controller_find({
            current,
            pageSize,
            levelName
        })
        ctx.body = res
    } catch (error) {
        console.log(error)
        ctx.body = serverError
    }

})



module.exports = router